<?php 

	session_start();
	include '../../function.php';

	
	if ($_POST) {
		// var_dump($_POST);exit;

		
		if (!preg_match("/^([a-zA-Z])+[.a-zA-Z0-9_-]*$/", $_POST['username'])) {

			$error ="用户名格式不正确，请重新输入";
			echo $error;exit;

		}else{
			$userdata = include './user.php';
			foreach ($userdata as $key => $value) {
				if (in_array($_POST['username'], $value)) {
					$error ="用户名已注册，请重新输入";
					echo $error;
				}
			}
		}

		if (isset($error)) {
			// exit;
		}else{
			if (!preg_match("/^([a-zA-Z0-9])+([.a-zA-Z0-9_-])*@([.a-zA-Z0-9_-])+([.a-zA-Z0-9_-]+)+([.a-zA-Z0-9_-])$/", $_POST['e-mail'])) {
				
			$error ="邮箱格式不正确，请重新输入";
			echo $error;
			}
		}


		
		if (isset($error)) {
			// exit;
		}else{
			if (!preg_match("/^134|135|137|138|156+[0-9]{8}$/", $_POST['phone'])) {
				
			$error ="电话格式不正确，请重新输入";
			echo $error;
			}
		}
		 
		
		if (isset($error)) {
			// exit;
		}else{
			if (!preg_match("/[.A-Za-z0-9_-]{6,}/", $_POST['password'])) {
				
			$error ="密码至少要输入6位，请重新输入";
			echo $error;
			}
		}

		if (isset($error)) {
			// exit;
		}else{
			if ($_POST['password']!=$_POST['to_true']) {
				
			$error ="密码不一致，请重新输入!";
			echo $error;
			}
		}


		if (isset($error)) {
			// exit;
		}else{
			if (isset($_POST['yanzheng'])) {
				if (strtolower($_POST['yanzheng'])!=$_SESSION['res_code']) {
					$error = "验证码有误，请重新输入";
					echo $error;
				}
			}
		}


		if (!isset($error)) {
				array_push($userdata, $_POST);
		
				$newuser = var_export($userdata,TRUE);
				$newuser = "<?php return $newuser ?>";
				// var_dump($newuser);
				file_put_contents('./user.php', $newuser);
				$message = "注册成功";
				include './tpl/success.html';
				exit;
			}
		

	}


 include './tpl/regist.html';


 ?>